package com.kitty.game.pet.handler;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.bean.PetObject;
import com.kitty.game.pet.message.RespRefineSuccess;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.util.NutMap;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

/**
 * 野生宠物洗炼
 */
@Component("ResetHandler")
@Slf4j
public class ResetHandler extends FeedPetHandler {

    private static final String SUCCESS = "恭喜，你的#Y{0}#n已成功蜕变为能力更强的宝宝。";
    private static final String GUIYUANLUNAME = "超级归元露";

    String canFeed(Pet pet, RoleEquip roleEquip, int num, PetObject petObject) {
        /**野生洗炼必须野生宠物 不是野生就返回*/
        if (isRightType(pet.getType())) {
            return MessageFormat.format(ERROR_PET_TYPE, pet.getName());
        }
        if (!roleEquip.getName().equals(GUIYUANLUNAME) || roleEquip.queryNum() < num) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, GUIYUANLUNAME);
        }
        if (petObject == null) {
            log.error("未找到宠物配置=={}", pet.getName());
            return MessageFormat.format(ERROR_PETSET, pet.getName());
        }
        return null;
    }

    @Override
    public String acceptFeed(Pet pet, Role role, RoleEquip roleEquip) {
        PetObject petObject = PetDataPool.getPetObject(pet.getName());
        String result = canFeed(pet, roleEquip, 1, petObject);
        if (result != null) {
            return result;
        }

        equipService.delEquipNum(role, roleEquip, 1);
        if (roleEquip.isEverLimit()) {
            petService.addLimitTime(pet, 1);
        }

        /**开始操作*/

        petService.setPetEffect(petObject, pet);
        pet.setLevel((short) 1);
        pet.setBasicAttrib();
        pet.setType(BABY);
        pet.setExp(0);
        pet.setCon((short) 0);
        pet.setWiz((short) 0);
        pet.setDex((short) 0);
        pet.setStr((short) 0);
        pet.setStr((short) 0);

        pet.setResistMetal(0);
        pet.setResistWood(0);
        pet.setResistWater(0);
        pet.setResistFire(0);
        pet.setResistEarth(0);

        pet.setResistConfusion(0);
        pet.setResistForgotten(0);
        pet.setResistPoison(0);
        pet.setResistSleep(0);
        pet.setResistFrozen(0);

        pet.setLastAtrrib(roleService.getLastAttributePoint(pet));
        pet.setReqLevel(petObject.getLevel_req());
        pet.setCurrLife(countService.countPetMaxLife(pet));
        pet.setCurrMana(countService.countPetMaxMana(pet));
        pet.setRefineMap(NutMap.NEW());

        MessagePusher.pushMessage(role, new RespRefineSuccess());
        pushOperTypeResult(role, true);
        petService.refreshPetInfo(role, pet);
        return MessageFormat.format(SUCCESS, pet.getName());
    }

    /**
     * 洗炼野生 只能是野生
     */
    @Override
    protected boolean isRightType(byte petType) {
        return petType != WILD;
    }
}
